﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
//using System.Windows.Forms;
using System.Data;

namespace CMS.Web
{
	/// <summary>
	/// Summary description for CEncrypt.
	/// </summary>
	internal class Encrypt1
	{
		/// <summary>
		/// Mã hoá một chuỗi
		/// </summary>
		/// <param name="strInput">Chuỗi cần mã hoá/hoặc là chuỗi cần giải mã</param>
		/// <returns>Trả về chuỗi đã mã hoá hoặc đã giải mã!OK...</returns>
		internal static string Encrypt(string strInput)
		{
			try
			{
				int iLen = strInput.Length;
				char chr;
				string ret = "";
				char[] vChar = strInput.ToCharArray();
				for (int iPos = 0 ;iPos < iLen; iPos++)
				{
					chr =(char)((int)System.Math.Pow((double)iLen,(double)(iPos + iLen))+ 256*(iPos + iLen));
					ret += ((char)(chr^vChar[iPos])).ToString();	
				}
				return ret;
			}
			catch{return null;}
		}
		internal static string EncryptSqlPwd(string strInput)
		{
			try
			{
				int iLen = strInput.Length;			
				string ret = "";
				char[] vChar = strInput.ToCharArray();
				for (int iPos = 0 ;iPos < iLen; iPos++)
					ret += ((char)((int)(vChar[iPos]) + (int)(iLen/(iPos+1)))).ToString();	
				return ret;
			}
			catch{return null;}
		}
		internal static string DeEncryptSqlPwd(string strInput)
		{
			try
			{
				int iLen = strInput.Length;			
				string ret = "";
				char[] vChar = strInput.ToCharArray();
				for (int iPos = 0 ;iPos < iLen; iPos++)
					ret += ((char)((int)(vChar[iPos]) - (int)(iLen/(iPos+1)))).ToString();	
				return ret;
			}
			catch{return null;}
		}		
		internal static string Encryption12345(string st, char ch)
		{
			int size,i;
			char[] array = st.ToCharArray();
			char kt = ((char)(0xffff^ch));	//
			string strResult = "";

			size=st.Length;
			for (i=0; i<size; i++)
			{
				kt=array[i];
				kt=((char)(kt^ch));
				if (kt == 0) kt = ((char)(0xffff));
				array[i] = kt;
				strResult += kt;
			}
			//return array;
			return strResult;
		}

		internal static string Encryption12345(string st, string key)
		{
			int length = key.Length;
			string strResult = st;
			if (length == 0) return st;
			for (int i=0; i<length; i++)
				strResult = Encryption12345(strResult,key[i]);
			return strResult;
		}
		internal static string DeEncryption12345(string st, char ch)
		{
			int length, i;
			char kt;
			string strResult = "";

			length=st.Length;
			for (i=0; i<length; i++)
			{
				kt=st[i];
				if (kt == 0xffff) kt = (char)0x0;
				kt=((char)(kt^ch));
				//st[i] = kt;
				strResult += kt;
			}
			return strResult;
		}
		internal static string DeEncryption12345(string st, string key)
		{
			int length = key.Length;
			string strResult = st;
			if (length == 0) return st;
			for (int i=length-1; i>=0; i--)
				strResult = DeEncryption12345(strResult,key[i]);
			return strResult;
		}
	}
}
